<template>
  <el-dialog :title="'创建收款单'" :model-value="dialog" @close="close">
    <el-form :model="formData" :rules="rules" ref="uploadForm" label-width="auto" style="padding: 0 20px" class="Dform">
      <el-form-item label="收款单月份" prop="settlementMonth">
        <el-date-picker v-model="formData.settlementMonth" value-format="YYYYMM" format="YYYY-MM" type="month"
          placeholder="请选择收款单月份" style="width: 100%;" />
      </el-form-item>
      <el-form-item label="合作方" prop="partnerUuid">
        <el-select v-model="formData.partnerUuid" placeholder="请选择" style="width: 100%;">
          <el-option label="所有合作方" value="ALL" />
          <el-option :label="item.partnerName" :value="item.partnerUuid" v-for="item in partnerList"
            :key="item.partnerUuid" />
        </el-select>
      </el-form-item>
      <el-form-item label="收款单周期" prop="months">
        <el-date-picker v-model="formData.months" type="daterange" range-separator="至" start-placeholder="开始日期"
          end-placeholder="结束日期" value-format="YYYY-MM-DD" format="YYYY-MM-DD" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit()">提交</el-button>
        <el-button @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, ref, PropType, watch, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { partnerQuery } from "@/api/user"
import { generateSettlement } from "@/api/settlement"
export default defineComponent({

  components: {},
  props: {
    dialog: {
      type: Boolean,
      default: false,
    },
    currentForm: {
      type: Object,
      default: () => { },
    },
    selectMenuList: {
      type: Array as PropType<{ title: string; id: string | number }[]>,
      default: () => [],
    },
  },
  emits: ['update:currentForm', 'update:dialog', 'refresh'],
  setup(props, context) {
    const formData = reactive(props.currentForm)
    const uploadForm = ref<any>(null)
    const partnerList = ref<any>([])

    watch(() => props.dialog,
      (val) => {
        Object.assign(formData, props.currentForm)
      }
    )

    const close = () => {
      context.emit('update:dialog', false)
      context.emit('refresh', { ...formData })
      context.emit('update:currentForm', {})
    }

    onMounted(() => {
      partnerQuery({ pageNum: 1, pageSize: 1000 }).then((res: any) => {
        partnerList.value = res.data.list || []
      })
    })

    const onSubmit = () => {
      uploadForm.value.validate((valid: boolean) => {
        if (valid) {
          generateSettlement({
            ...formData,
            settlementPeriodStart: formData.months[0],
            settlementPeriodEnd: formData.months[1]
          }).then((res: any) => {
            if (res.code == 200) {
              ElMessage.success('创建成功')
              close()
            }
          })
        } else {
          ElMessage({
            message: '验证失败',
            type: 'error',
            duration: 1000,
          })
          return false
        }
      })
    }

    const resetForm = () => {
      uploadForm.value.resetFields()
      context.emit('update:currentForm', {})
    }

    return {
      uploadForm,
      formData,
      partnerList,
      close,
      resetForm,
      onSubmit,
      rules: {
        months: [
          { required: true, message: '请选择收款单周期', trigger: 'blur' }
        ],
        settlementMonth: [
          { required: true, message: '请选择收款单月份', trigger: 'blur' }
        ],
        partnerUuid: [
          { required: true, message: '请选择合作方', trigger: 'blur' }
        ],
      },
    }
  },
})
</script>

<style></style>
